﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace TA.Web.Utils
{
	public class FormsAuthentication
	{
		public static void SetAuthCookie(string userName, bool createPersistentCookie)
		{
			System.Web.Security.FormsAuthentication.SetAuthCookie(userName, createPersistentCookie);
			HttpContext.Current.Items[UserKey] = userName;
		}
		public static void SignOut()
		{
			System.Web.Security.FormsAuthentication.SignOut();
			HttpContext.Current.Items[UserKey] = false;
		}
		public static bool IsAuthenticated
		{
			get
			{
				if (object.Equals(HttpContext.Current.Items[UserKey], false))
					return false;
				if (HttpContext.Current.Items[UserKey] is string)
					return true;

				return HttpContext.Current.Request.IsAuthenticated;
			}
		}
		public static string UserName
		{
			get
			{
				if (object.Equals(HttpContext.Current.Items[UserKey], false))
					return null;
				if (HttpContext.Current.Items[UserKey] is string)
					return (string)HttpContext.Current.Items[UserKey];

				return HttpContext.Current.User.Identity.Name;
			}
		}

		const string UserKey = "TA.Web.Utils.FormsAuthentication.User";
	}
}
